Skip to content

Fix #5114 - Avoid same-symbol qualifier-difference case falling into wrong cast branch#5117

Open
gulugulubing wants to merge 3 commits intoldc-developers:masterfrom
gulugulubing:issue5114
Open

Fix #5114 - Avoid same-symbol qualifier-difference case falling into wrong cast branch#5117
gulugulubing wants to merge 3 commits intoldc-developers:masterfrom
gulugulubing:issue5114

Conversation

@gulugulubing
Copy link
Copy Markdown
Contributor

@gulugulubing gulugulubing commented Apr 20, 2026

It is related to #5114. The root cause is the const(I) -> I contract-context conversion always called DtoCastClass, then due to I.isBaseOf(I, ...) return false, the program accidently fall into the DtoDynamicCastInterface which is ObjC-only and ends in unreachable for non-ObjC.

So I added an early guard for same-symbol qualifier-difference case there.

The patch is only in tocall.cpp, while objcgen.cpp and classes.cpp are modified just because of removing trailing blank.

Copy link
Copy Markdown
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the whitespace changes

Comment thread gen/tocall.cpp Outdated
@gulugulubing
Copy link
Copy Markdown
Contributor Author

please remove the whitespace changes

OK, is it accpetable:
For files with actual content changes, I kept the trailing whitespace cleanup (since we're already modifying these files)
For files with only whitespace changes, I reverted them completely to avoid unnecessary diffs.

Comment thread gen/tocall.cpp
thisptrLval = DtoAllocaDump(DtoCastClass(loc, dthis, iface->type));
auto thisVal = DtoLoad(DtoType(thistype), thisptrLval);
DImValue dthis(thistype, thisVal);
thisptrLval = DtoAllocaDump(DtoCastClass(loc, &dthis, iface->type));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like no change was made here, except for the dangerous-looking change from heap allocated dthis to stack allocated. Possibly use-after-scope. Revert this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the original heap allocated dthis cause potential leak-like lifetime?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's for you to prove ;)
A mem leak is much more benign than a use-after-scope bug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original heap version, there are no deallocation in the caller and callee, so I think there is a mem leak.

And use-after-scope does not happen, because DtoCastClass only consumes the pointer immediately and does not store it.

At last, I am a little confused here: do you mean the original code intentionlly choose mem-leak to avoid use-after-scope?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In classes.cpp, there is similar stack allocation:

    // get class ptr
    LLValue *v = DtoRVal(val);
    // cast to size_t
    v = gIR->ir->CreatePtrToInt(v, DtoSize_t(), "");
    // cast to the final int type
    DImValue im(Type::tsize_t, v);
    return DtoCastInt(loc, &im, _to);

So I am not sure which is best practise here.

Comment thread tests/codegen/gh5114.d
// True dynamic interface casts (different interface symbols) are still valid
// and are covered below.
//
// RUN: %ldc -c %s
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: line 11 already tests compilation, so you can remove line 10

@gulugulubing gulugulubing changed the title Fix #5114 - Avoid same-symbol qualifier-difference case falling into DtoCastClass Fix #5114 - Avoid same-symbol qualifier-difference case falling into wrong cast branch Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants